package com.glodon.paas.account.api.sso.web;

import com.google.common.collect.LinkedListMultimap;
import com.google.common.collect.ListMultimap;
import com.google.common.collect.Multimaps;

import javax.servlet.http.HttpSession;

/**
 * @author Don Li
 */
public class InMemoryAuthenticatedSessionRegistry implements AuthenticatedSessionRegistry {
    private final ListMultimap<String, HttpSession> authenticatedSession =
            Multimaps.synchronizedListMultimap(LinkedListMultimap.<String, HttpSession>create());

    @Override
    public void invalidateUserSessions(String id) {
        for (HttpSession session : authenticatedSession.get(id)) {
            session.invalidate();
        }
        authenticatedSession.removeAll(id);
    }

    @Override
    public void addSession(String id, HttpSession session) {
        authenticatedSession.put(id, session);
    }

    @Override
    public void removeSession(String id, HttpSession session) {
        authenticatedSession.remove(id, session);
    }
}
